package com.a85.wxhelper.http;

import com.a85.wxhelper.bean.GetCurrentTime;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author hejiangwei
 * Created at 2018/7/17.
 */
public class HttpApi {
    private Retrofit retrofit;
    private OkHttpClient okHttpClient;
    private int  DEFAULT_TIMEOUT=20;

    private static HttpApi sInstance = null;
    public static HttpApi getInstance(){
        if (sInstance == null) {
            sInstance = new HttpApi();

        }

        return sInstance ;
    }
    public static String  BASE_URL= "http://www.plantsapp.cn/api/v1/";
    public static final String  URL_GETCurrentTime = "getCurrentTime";

    public HttpApi() {
        okHttpClient = new OkHttpClient.Builder()
                .addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
//                .cache(cache)
//                .addInterceptor(CacheInterceptor(context))
//                .addNetworkInterceptor(CacheInterceptor(context))
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();
        retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();
    }


    public Observable<GetCurrentTime> getVersion() {


        return retrofit.create(HttpApiInterface.class).getCurrentTime(1, SecretUtils.byMD5Code("kewo_plants_android") + (int)(System.currentTimeMillis() / 1000));
    }


}
